package OOP.day10.AnimalDemo04;

/**
 * 主人类，可以喂养动物
 */
public class Master {
    void feed(Animal animal) { //传入Animal父类，传参时可以传Animal类及其子类
        animal.eat();
    }

    public static void main(String[] args) {
        Animal animal01 = new Dog("旺财", 3, "红");
        Animal animal02 = new Chick();
        Animal animal03 = new Fish();
        Master master = new Master();
        master.feed(animal01);//喂狗
        animal01.eat();
        master.feed(animal02);//喂鸡
        master.feed(animal03);//喂鱼

        System.out.println("------------------------");
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("小黑", 2, "黑");
        animals[1] = new Chick("小红", 1, "红");
        animals[2] = new Fish("小蓝", 1, "蓝");
        animals[3] = new Dog("小白", 1, "白");
        animals[4] = new Chick("小绿", 1, "绿");
        for (Animal animal : animals) {
            System.out.println(animal.name);
            if (animal instanceof Dog) {
                Dog dog = (Dog) animal;
                dog.lookHome();
                dog.swim();
            } else if (animal instanceof Chick) {
                Chick chick = (Chick) animal;
                chick.layEggs();
            } else if (animal instanceof Fish) {
                Fish fish = (Fish) animal;
                fish.eat();
            } else if (animal instanceof Swim) {
                Swim swim = (Swim) animal;
                swim.swim();
            }
        }

        System.out.println("------------------------");
        //向下转型
        Animal dog = new Dog();
        if (dog instanceof Dog) { //true
            Dog dog1 = (Dog) dog; //引用dog所指向的对象Dog，就是Dog当前类型
        }
        if (dog instanceof Swim) { //true
            Swim swimDog = (Swim) dog;//引用dog所指向的对象Dog，实现了Swim接口
        }


    }
}
